<template>
  <a-alyout>
    <a-alyout-content
      :style="{
        padding: '24px',
        margin: 0,
        minHeight: '200px',
      }"
    >
      <a-row>
        <a-col :span="6">
          <a-tree
            v-if="level1.length > 0"
            :tree-data="level1"
            @select="onSelect"
            :replaceFields="{ title: 'name', key: 'id', value: 'id' }"
            :defaultExpandAll="true"
          >
          </a-tree>
        </a-col>
        <a-col :span="18">
          <!-- 显示文档的阅读数和点赞数 -->
          <div>
            <h2>{{doc.name}}</h2>
            <div>
              <span>阅读数：{{doc.viewCount}}</span>&nbsp;&nbsp;
              <span>阅读数：{{doc.voteCount}}</span>
              <a-divder style="height: 2px;background-color: #9999cc;" />
            </div>
          </div>
          <div :innerHTML="html"></div>
          <div class="vote-div">
            <a-button type="primary" shape="round" :size="'large'" @click="vote">
              <template #icon>
                <linkOutline />&nbsp;点赞：{{doc.voteCount}}
              </template>
            </a-button>
          </div>
        </a-col>
      </a-row>
    </a-alyout-content>
  </a-alyout>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, createVNode } from "vue";
import axios from "axios";
import { message } from "ant-design-vue";
import { Tool } from "../util/tool"
import { useRoute } from "vue-router";
export default defineComponent({
  name: "AdminDoc",
  setup() {
    // 通过路由内置函数得到一个路由的变量route，而这个变量里面就有路由的信息，包括路径参数
    const route = useRoute();
    const docs = ref();
    const html = ref();

    const defaultSelectedKeys = ref();
    defaultSelectedKeys.value = [];
    const doc = ref();
    doc.value = {};

    /**
     * 一级文档树的数据层级说明，children属性就是二级文档
     * [{
     *   id: "",
     *   name: "",
     *   children: [{
     *     id: "",
     *     name: "",
     *   }]
     * }]
     */
    const level1 = ref(); // 一级文档树，children属性就是二级文档
    level1.value = [];
    /**
     * 数据查询
     **/
    const handleQuery = () => {
      axios.get("/doc/getDocByEbookId/"+route.query.ebookId).then((response) => {
        const data = response.data;
        if (data.success) {
          docs.value = data.content;

          level1.value = [];
          level1.value = Tool.array2Tree(docs.value, 0);
        } else {
          message.error(data.message);
        }
      });
    };

    /**
     * 内容查询
     */
    const  handleQueryContent = (id:number)=>{
        axios.get("/doc/find-content/"+id).then((response)=>{
          const data=response.data;
            if(data.success){
                html.value = data.content;
            }else{
                message.error(data.message);
            }
        });
    };

    const onSelect = (selectedKeys:any,info:any)=>{
        console.log('selected',selectedKeys,info);
        if (Tool.isNotEmpty(selectedKeys)) {
            // 加载内容
            handleQueryContent(selectedKeys[0]);
        }
    };

    const vote=()=>{
      axios.get('/doc/vote/'+doc.value.id).then((response)=>{
        const data = response.data;
        if (data.success) {
          doc.value.voteCount++;
        }else{
          message.error(data.message);
        }
      })
    };

    onMounted(() => {
      handleQuery();
    });
    return {
      level1,
      html,
      onSelect,
      defaultSelectedKeys,
      doc,
      vote,
    };
  },
});
</script>

<style>
  .vote-div{
    padding: 15px;
    text-align: center;
  }
</style>